from django.core.cache import cache
from django.shortcuts import render

# Create your views here.
from home import models,homeser
from rest_framework.views import APIView
from rest_framework.response import Response

#导航接口
# class NavView(APIView):
#   def get(self,request):
#     #1.设置一个缓存key
#     navkey = 'navlist'
#     #2.根据这个key去缓存中获取数据
#     catchdata = cache.get(navkey)
#     #3.判断缓存中是否有数据
#     if catchdata:
#       return Response({"msg":"ok","data":catchdata})
#     pos = request.query_params.get('pos')
#     navlist = models.NavModel.objects.filter(position=pos, isshow=True).order_by("order")
#     ser = homeser.NavSer(navlist,many=True)
#     #4.将数据存入缓存中
#     cache.set(navkey,ser.data,60*60)
#     return Response({"msg": "ok", "data": ser.data})

class TopNavView(APIView):
  def get(self,request):
    #1.设置一个缓存key
    navkey = 'topnavlist'
    #2.根据这个key去缓存中获取数据
    catchdata = cache.get(navkey)
    #3.判断缓存中是否有数据
    if catchdata:
      return Response({"msg":"ok","data":catchdata})
    navlist = models.NavModel.objects.filter(position=1, isshow=True).order_by("order")
    ser = homeser.NavSer(navlist,many=True)
    #4.将数据存入缓存中
    cache.set(navkey,ser.data,60*60)
    return Response({"msg": "ok", "data": ser.data})

class BottomNavView(APIView):
  def get(self,request):
    #1.设置一个缓存key
    navkey = 'bottomnavlist'
    #2.根据这个key去缓存中获取数据
    catchdata = cache.get(navkey)
    #3.判断缓存中是否有数据
    if catchdata:
      return Response({"msg":"ok","data":catchdata})
    navlist = models.NavModel.objects.filter(position=0, isshow=True).order_by("order")
    ser = homeser.NavSer(navlist,many=True)
    #4.将数据存入缓存中
    cache.set(navkey,ser.data,60*60)
    return Response({"msg": "ok", "data": ser.data})


#轮播图接口
class BannerView(APIView):
 def get(self,request):
    banner = models.BannerModel.objects.filter(isshow=True).order_by("order")
    ser = homeser.BannerSer(banner,many=True)
    return Response({"msg": "ok", "data": ser.data})